package com.quectel.leshan.client;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.response.ExecuteResponse;
import org.eclipse.leshan.core.response.ObserveResponse;
import org.eclipse.leshan.core.response.ReadResponse;
import org.eclipse.leshan.core.response.WriteResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class ConnectivityResource extends BaseInstanceEnabler {

    private static final Logger LOG = LoggerFactory.getLogger(ConnectivityResource.class);

    private static final Random RANDOM = new Random();

    public void changeResource() {
        //触发相应Resource改变的监听事件，如果服务端发起了该resourceId的ObserveRequest，则服务端会收到资源变化的通知，重新读取该资源值
        fireResourcesChange(RANDOM.nextInt(9));
    }

    @Override
    public ReadResponse read(ServerIdentity identity, int resourceid) {
        log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceid);

        switch (resourceid) {
            case 0:
                return ReadResponse.success(resourceid, 1);
            case 1:
                return ReadResponse.success(resourceid, 2);
            case 2:
                return ReadResponse.success(resourceid, 3);
            case 3:
                return ReadResponse.success(resourceid, 4);
            case 4:
                return ReadResponse.success(resourceid,"5");
            case 5:
                return ReadResponse.success(resourceid, "6");
            case 6:
                return ReadResponse.success(resourceid, 7);
            case 7:
                return ReadResponse.success(resourceid, "8");
            case 8:
                return ReadResponse.success(resourceid, 21653);
            case 9:
                return ReadResponse.success(resourceid, 36);
            case 10:
                return ReadResponse.success(resourceid, 98);
            default:
                return super.read(identity, resourceid);
        }
    }

    @Override
    public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
        String withParams = null;
        if (params != null && params.length() != 0) {
            withParams = " with params " + params;
        }
        log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceid,
                withParams != null ? withParams : "");

        if (resourceid == 4) {
            new Timer("Reboot Lwm2mClient").schedule(new TimerTask() {
                @Override
                public void run() {
                    getLwM2mClient().stop(true);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                    getLwM2mClient().start();
                }
            }, 500);
        }
        return ExecuteResponse.success();
    }

    @Override
    public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResource value) {
        log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceid);

        switch (resourceid) {
            case 3:
                fireResourcesChange(resourceid);
                return WriteResponse.success();
            case 13:
                return WriteResponse.notFound();
            case 14:
                return WriteResponse.success();
            case 15:
                return WriteResponse.success();
            default:
                return super.write(identity, resourceid, value);
        }
    }

    @Override
    public ObserveResponse observe(ServerIdentity identity) {
        // Perform a read by default
        ReadResponse readResponse = this.read(identity);
        return new ObserveResponse(readResponse.getCode(), readResponse.getContent(), null, null,
                readResponse.getErrorMessage());
    }

    @Override
    public ObserveResponse observe(ServerIdentity identity, int resourceid) {
        // Perform a read by default
        ReadResponse readResponse = this.read(identity, resourceid);
        return new ObserveResponse(readResponse.getCode(), readResponse.getContent(), null, null,
                readResponse.getErrorMessage());
    }
}
